package com.ruoyi.demo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.AuctionDepositMapper;
import com.ruoyi.demo.domain.AuctionDeposit;
import com.ruoyi.demo.service.IAuctionDepositService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.transaction.annotation.Transactional;

/**
 * 竞拍保证金Service业务层处理
 * 
 * @author metaee
 * @date 2025-03-05
 */
@Service
public class AuctionDepositServiceImpl implements IAuctionDepositService 
{
    @Autowired
    private AuctionDepositMapper auctionDepositMapper;

    /**
     * 查询竞拍保证金
     * 
     * @param depositId 竞拍保证金主键
     * @return 竞拍保证金
     */
    @Override
    public AuctionDeposit selectAuctionDepositByDepositId(Long depositId)
    {
        return auctionDepositMapper.selectAuctionDepositByDepositId(depositId);
    }

    /**
     * 查询竞拍保证金列表
     * 
     * @param auctionDeposit 竞拍保证金
     * @return 竞拍保证金
     */
    @Override
    public List<AuctionDeposit> selectAuctionDepositList(AuctionDeposit auctionDeposit)
    {
        return auctionDepositMapper.selectAuctionDepositList(auctionDeposit);
    }

    /**
     * 新增竞拍保证金
     * 
     * @param auctionDeposit 竞拍保证金
     * @return 结果
     */
    @Override
    public int insertAuctionDeposit(AuctionDeposit auctionDeposit)
    {
        auctionDeposit.setCreateTime(DateUtils.getNowDate());
        return auctionDepositMapper.insertAuctionDeposit(auctionDeposit);
    }

    /**
     * 修改竞拍保证金
     * 
     * @param auctionDeposit 竞拍保证金
     * @return 结果
     */
    @Override
    public int updateAuctionDeposit(AuctionDeposit auctionDeposit)
    {
        auctionDeposit.setUpdateTime(DateUtils.getNowDate());
        return auctionDepositMapper.updateAuctionDeposit(auctionDeposit);
    }

    /**
     * 批量删除竞拍保证金
     * 
     * @param depositIds 需要删除的竞拍保证金主键
     * @return 结果
     */
    @Override
    public int deleteAuctionDepositByDepositIds(Long[] depositIds)
    {
        return auctionDepositMapper.deleteAuctionDepositByDepositIds(depositIds);
    }

    /**
     * 删除竞拍保证金信息
     * 
     * @param depositId 竞拍保证金主键
     * @return 结果
     */
    @Override
    public int deleteAuctionDepositByDepositId(Long depositId)
    {
        return auctionDepositMapper.deleteAuctionDepositByDepositId(depositId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int payDeposit(AuctionDeposit deposit) {
        // 1. 校验保证金信息
        if (deposit.getAmount() == null || deposit.getAmount().compareTo(java.math.BigDecimal.ZERO) <= 0) {
            throw new ServiceException("保证金金额必须大于0");
        }
        
        // 2. 设置支付状态为已支付
        deposit.setPayStatus("1");
        deposit.setPayTime(DateUtils.getNowDate());
        
        // 3. 生成交易流水号
        deposit.setTransactionNo("PAY" + System.currentTimeMillis());
        
        // 4. 保存保证金记录
        return auctionDepositMapper.insertAuctionDeposit(deposit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int refundDeposit(Long depositId) {
        // 1. 查询保证金记录
        AuctionDeposit deposit = auctionDepositMapper.selectAuctionDepositByDepositId(depositId);
        if (deposit == null) {
            throw new ServiceException("保证金记录不存在");
        }
        
        // 2. 校验状态
        if (!"1".equals(deposit.getPayStatus())) {
            throw new ServiceException("只有已支付的保证金才能退款");
        }
        
        // 3. 更新退款状态
        deposit.setPayStatus("2");
        deposit.setRefundTime(DateUtils.getNowDate());
        
        // 4. 保存退款记录
        return auctionDepositMapper.updateAuctionDeposit(deposit);
    }
}
